package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器实例化对象
 */

public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p1=new Person();
        System.out.println(p1);

        Person p2=new Person("李四",55);
        System.out.println(p2);

        //1获取类对象
        Class cls = Class.forName("reflect.Person");
        /*
            2
            java.lang.reflect.Constructor
            Constructor的每一个实例用于表示一个构造器,使用它可以相当于调用该构造器实例化对象
         */
//        Constructor c =  cls.getConstructor();//获取默认无参的构造器
//        c.newInstance();//不需要传入实参

        //Person(String,int)
        Constructor c =  cls.getConstructor(String.class,int.class);
        Object obj = c.newInstance("王五",33); //new Person("王五",33);
        System.out.println(obj);
    }
}
